﻿using Angle.Krill.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Angle.Krill.WinFile
{
    public partial class NewFile : ShadowForm
    {
        private string folderPath;

        public NewFile(string folderPath)
        {
            InitializeComponent();

            this.Initialize();

            this.folderPath = folderPath;
        }

        private void Initialize()
        {
            this.Icon = ImageHelper.ConvertToIcon(ImageHelper.FileIcon);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string fileName = this.txtFileName.Text.Trim();
            if (string.IsNullOrEmpty(fileName))
            {
                this.txtFileName.Focus();
            }

            string filePath = Path.Combine(this.folderPath, fileName);
            FileHelper.CreateFile(filePath, () =>
            {
                AntdUI.Modal.open(new AntdUI.Modal.Config(this, "提示", "已存在相同文件名称，是否进行覆盖？", AntdUI.TType.Warn)
                {
                    OnOk = config =>
                    {
                        FileHelper.DelFile(filePath);

                        this.btnOk_Click(sender, e);

                        return true;
                    }
                });
            }, () =>
            {
                this.Success( "文件创建成功");
            });
        }
    }
}
